import axios from 'axios'

// 创建一个 axios 实例
const service = axios.create({
  // 请求超时时间
  timeout: 3000
})

// 配置请求的跟路径
service.defaults.baseURL = 'http://121.5.221.199:8080/back/'
service.defaults.headers.put['Content-Type'] = 'application/json;charset=UTF-8;multipart/form-data'
// 添加请求拦截器
service.interceptors.request.use(
  config => {
    const token = window.sessionStorage.getItem('token')
    if (token) {
      config.headers.token = token
    }
    return config
  },
  error => {
    console.log('请求拦截器:', error)
  }
)

// 添加响应拦截器
service.interceptors.response.use(
  response => {
    if (response.headers.token) {
      console.log('更换token?')
      sessionStorage.setItem('token', response.headers.token)
    }
    return response
  },
  error => {
    console.log('响应拦截器:', error)
  }
)

export default service
